﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BakePlane : MonoBehaviour {

    public string prefabName = "Cube";

    public int planeScale = 20; //Plane上的1对应多少个Cube

    public float cubeScale = 0.5f;    //cube的缩放比例

    private Vector2 offset; //初始位置

	// Use this for initialization
	void Awake () {
        InitPlane();
	}

    public void InitPlane()
    {
        GameObject prefab = AssetsManager.instance.GetAssetByName(prefabName) as GameObject;

        //初始位置为地面长的一半+cube长的一半，宽也是一样
        offset = new Vector2((-transform.localScale.x * 10) / 2 + cubeScale / 2, (-transform.localScale.z * 10) / 2 + cubeScale / 2);

        //计算地图长宽（长度可以放多少个Cube，宽度可以放多少个Cube）
        int mapLength = (int)transform.localScale.x * planeScale;
        int mapWidth = (int)transform.localScale.z * planeScale;

        //实例化数组
        AstarUtility.Instance.cubeGrids = new CubeGrid[mapLength,mapWidth];

        for (int i = 0; i < mapLength; i++)
        {
            for (int j = 0; j < mapWidth; j++)
            {
                GameObject go = Instantiate(prefab, new Vector3(offset.x + i * cubeScale, 0, offset.y + j * cubeScale), Quaternion.identity);
                go.GetComponent<CubeGrid>().x = i;
                go.GetComponent<CubeGrid>().y = j;

                //将当前网格报错
                AstarUtility.Instance.cubeGrids[i, j] = go.GetComponent<CubeGrid>();
            }
        }
    }

}
